package com.geek.netty.inbound;

import com.geek.netty.filter.HeaderHttpRequestFilter;
import com.geek.netty.filter.HttpRequestFilter;
import com.geek.netty.outbound.httpclient4.HttpOutboundHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.ReferenceCountUtil;

import java.util.List;

/**
 * @program geek
 * @description: handler
 * @author: chanzhugang
 * @create: 2021/04/03 00:28
 */
public class HttpInboundHandler extends ChannelInboundHandlerAdapter {


    private List<String> proxyService;

    private HttpOutboundHandler handler;

    private HttpRequestFilter filter = new HeaderHttpRequestFilter();

    public HttpInboundHandler(List<String> proxyService) {
        this.proxyService = proxyService;
        // 构造出站对象，设置相关属性值
        this.handler = new HttpOutboundHandler(this.proxyService);
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }


    @Override
    public void channelRead(ChannelHandlerContext cxt, Object msg) {
        try {
            FullHttpRequest fullHttpRequest = (FullHttpRequest) msg;
            // 出站处理
            handler.handle(fullHttpRequest, cxt, filter);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ReferenceCountUtil.release(msg);
        }

    }

}